package practice3;

public class Arithmetic {
    private int num1;
    private int num2;
    private char operator;
    private int result;

    // 无参构造
    public Arithmetic() {}

    // 有参构造（初始化后自动计算结果）
    public Arithmetic(int num1, int num2, char operator) {
        this.num1 = num1;
        this.num2 = num2;
        this.operator = operator;
        this.calculate();
    }

    // 计算结果
    public void calculate() {
        if (operator == '+') {
            result = num1 + num2;
        } else if (operator == '-') {
            result = num1 - num2;
        } else {
            throw new IllegalArgumentException("运算符只能是'+'或'-'");
        }
    }

    // getter方法
    public int getNum1() { return num1; }
    public int getNum2() { return num2; }
    public char getOperator() { return operator; }
    public int getResult() { return result; }

    // 转换为算式字符串
    @Override
    public String toString() {
        return num1 + " " + operator + " " + num2 + " = " + result;
    }
}
